﻿using System.Collections.Generic;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XNA_2DFramwork;

namespace ArtificialIntelligence.CombatComponent
{
    public class EnemySensor : Sensor
    {
        public List<VisualObject> List { get; set; }
        private Vector2 _origin;
       // public SensorState sensorState;
        public SensedItem sensedItem;
        private int MinHealth { set; get; }
        
        public EnemySensor(Game game,PhysicsSimulator PS ,Vector2 position,GraphicsDevice graphicsDevice,int Range) : base(game)
        {
            PhysicsSimulator = PS;
            Position = position;
            sensedItem = new SensedItem {SensorState = SensorState.Safe};
            //---------------------------------------------
            SpriteBatch = new SpriteBatch(graphicsDevice);
            Texture = DemoBaseXNA.DrawingSystem.DrawingHelper.CreateCircleTexture(graphicsDevice, Range, Color.TransparentBlack);  
            _origin = new Vector2(Texture.Width / 2f, Texture.Height / 2f);

            Body = BodyFactory.Instance.CreateCircleBody(PhysicsSimulator, Range, 0.5f);
            Body.Position = Position;

            Geom = GeomFactory.Instance.CreateCircleGeom(PhysicsSimulator, Body, Range, 10);

            Geom.CollisionResponseEnabled = false;
            
        }

        public override void Update()
        {
            Position=Body.Position ;
        }
        
        public bool IsSeeingEnemy()
        { 
           var x = _lVisualObject;
            for (int i = 0; i < _lVisualObject.Count; i++)
            {
                string s = _lVisualObject[i].GetType().Name;
                if (s == "Player")
                {
                    if (AABB.Intersect(ref Geom.AABB, ref _lVisualObject[i].Geom.AABB))
                    {
                        sensedItem = new SeeingEnemySensed();
                        sensedItem.SensorState = SensorState.SeeingEnemy;
                        return true;
                    }
                }
            }
           
            return false;
        }

        public bool IsSeriouslyDamaged(int health)
        {
            if (MinHealth > health)
            {
                //NeedDecision = true;
                sensedItem = new SeriousDamageSensed();
                sensedItem.SensorState = SensorState.SeriousDamage;
                return true; 
            }

           // NeedDecision = false;
            sensedItem = new SafeSensed();
            sensedItem.SensorState = SensorState.Safe;
            return false;
        }

        public SensorState GetState()
        {
            return sensedItem.SensorState;
        }

        public override void Draw()
        {
            SpriteBatch.Begin();
            SpriteBatch.Draw(Texture, Geom.Position, null, Color.TransparentWhite, Body.Rotation, _origin, 1f, SpriteEffects.None, 0f);
            SpriteBatch.End();
        }

        //public void UpdateSensedItem(SensorState State)
        //{
        //    switch (State)
        //    {
        //        case SensorState.Safe:
        //            {
        //                sensedItem = new SafeSensed();
        //            }
        //            break;
        //        case SensorState.SeeingEnemy:
        //            {
                        
        //            }
        //            break;
        //        case SensorState.SeriousDamage:
        //            {
        //                sensedItem 
        //            }
        //            break;
        //        default:
        //            throw new ArgumentOutOfRangeException("State");
        //    }
        //}
    }
}
